<?php
require '../../../../framework/bootstrap.inc.php';
require '../../../../addons/hdz/function.php';

ini_set('display_errors', 'On');
error_reporting(E_ALL ^ E_NOTICE);
define('IN_MOBILE', true);
$input = file_get_contents('php://input');
if (!empty($input) && empty($_GET['out_trade_no'])) {
    
    $obj  = simplexml_load_string($input, 'SimpleXMLElement', LIBXML_NOCDATA);
    
    $data = json_decode(json_encode($obj), true);
    if (empty($data)) {
        exit('fail');
    }
    if ($data['result_code'] != 'SUCCESS' || $data['return_code'] != 'SUCCESS') {
        exit('fail');
    }
    
    $get = $data;
    $order['sn'] = $get['out_trade_no'];
    $order = pdo_fetch("select * from " . tablename('hdz_order') . ' where ordersn=:ordersn  limit 1', array(
        'ordersn' => $order['sn'],
    ));
    if(!$order)
    {
        exit('fail');
    }
    if($order['status'] != 1)
    {
        exit('fail');
    }

    pdo_update('hdz_order',array(
            'status' => 2,
            'paytime' => time()
        ),array(
            'id' => $order['id'],
        )
    );//更新订单支付状态
    $condition = array('id' => $order['ticketid']);
    $ticket = pdo_fetch("SELECT * FROM " .tablename('hdz_infoticket'). " WHERE id = {$order['ticketid']}");
    $number = pdo_fetchcolumn("SELECT COUNT(*) FROM " .tablename('hdz_orderticket'). " WHERE orderid = {$order['id']}");
    if($ticket['ticketcount'] || $ticket['ticketlast'])//如果库存做了限制
    {
        $ticket['ticketlast'] = $ticket['ticketlast'] - $number;
        pdo_update('hdz_infoticket',array('ticketlast' => $ticket['ticketlast']),$condition);//更新票的库存
    }
    //生成活动票码并保存
    $ticketcodemsg = '';
    for ($i=0; $i < $number; $i++) { 
        $ticketcode = 'HD'.date('YmdHis') . $order['id'] . $i;//活动票码
        $data = array(
            'ticketcode' => $ticketcode,
            'sendertime' => time(),
            'ticketstatus' => 1,
        );
        if($i == 0)
        {
            $ticketcodemsg = $ticketcode;
        }
        else
        {
            $ticketcodemsg = $ticketcodemsg."和".$ticketcode;    
        }

        $getid = pdo_fetchcolumn("SELECT id FROM " .tablename('hdz_orderticket'). " WHERE orderid = {$order['id']} and ticketstatus=0");
        if($getid)
        {
           pdo_update('hdz_orderticket',$data,array('id'=>$getid)); 
        }    
    }
    
    $mobile = pdo_fetchcolumn(
        "SELECT fieldvalue FROM ".tablename('hdz_orderform')." WHERE 
        catalogid = 90 and orderid = {$order['id']}"
    );//电话


    $where = " id = :id";
    $params = array(
        ':id' => $order['infoid'],
    );
    $sql = "select * from " . tablename('hdz_info') . " where $where";
    $info = pdo_fetch($sql,$params);
    if($info['status'] != 1 && $info['status'] != 2)
    {
        exit('fail');
    }

    //13554002130
    if($mobile)
    {
        hdzSendDX($mobile,"您好，您参加的的活动【".$info['title']."】报名成功,请到活动站-我参与的页面领取二维码入场票。");//发送短信
    }
    exit('success');
} else {
    $get = $_GET;
}



exit('fail');